package com.kk.medical_checkitem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kk.medical_checkitem.dao.CheckgroupCheckitemDao;
import com.kk.medical_checkitem.dao.TCheckitemDao;
import com.kk.medical_checkitem.entity.CheckgroupCheckitem;
import com.kk.medical_checkitem.entity.TCheckitem;
import com.kk.medical_checkitem.service.TCheckitemService;
import com.kk.medical_checkitem.util.QueryPageBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yjq
 * @since 2023-02-06
 */
@Service
public class TCheckitemServiceImpl implements TCheckitemService {
    @Resource
    private TCheckitemDao tCheckitemMapper;
    @Resource
    private CheckgroupCheckitemDao checkgroupCheckitemDao;

    @Override
    public Page<TCheckitem> findall(QueryPageBean queryPageBean) {
        Page<TCheckitem> p1 = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        LambdaQueryWrapper<TCheckitem> qq = new LambdaQueryWrapper<>();
        ;
        if (queryPageBean.getQueryString() != null) {

            qq.like(TCheckitem::getName, queryPageBean.getQueryString());
            qq.or();
            qq.like(TCheckitem::getCode, queryPageBean.getQueryString());
        }
        qq.orderByDesc(TCheckitem::getCode);
        Page<TCheckitem> p2 = tCheckitemMapper.selectPage(p1, qq);
        return p2;
    }


    @Override
    public int saveCheckItem(TCheckitem tCheckitem) {
        return tCheckitemMapper.insert(tCheckitem);
    }

    @Override
    @Transactional
    public int deleteCheckItem(Integer id) {
        LambdaQueryWrapper<CheckgroupCheckitem> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CheckgroupCheckitem::getCheckitemId, id);
        checkgroupCheckitemDao.delete(lqw);
        tCheckitemMapper.deleteById(id);
        return 1;
    }

    @Override
    public int updateCheckItem(TCheckitem tCheckitem) {
        return tCheckitemMapper.updateById(tCheckitem);
    }

    @Override
    public TCheckitem findById(Integer id) {
        return tCheckitemMapper.selectById(id);
    }


}
